﻿//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="billfellows.net">
//     I mention copyright so StyleCop is happy.
// </copyright>
//-----------------------------------------------------------------------
namespace ReputationMinder
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    /// <summary>
    /// I love it when a program comes together.
    /// </summary>
    public class Program
    {
        /// <summary>
        /// Go go gadget reputation minder!
        /// </summary>
        /// <param name="args">Command line arguments. None used</param>
        public static void Main(string[] args)
        {
            Properties.Settings settings = new Properties.Settings();
            Dictionary<int, string> watchList = new Dictionary<int, string>();

            int userid = 0;
            string userUrl = string.Empty;
            string[] kvp;

            // blerg because there seems to be no elegant way of storing the people and places.
            // System.Collections.Specialized.StringDictionary seemed an option except it doesn't
            // serialize natively and the desire is for people to be able to edit the config
            foreach (string blerg in settings.WatchList.Split(new char[] { ';' }))
            {
                kvp = blerg.Split(new char[] { '|' });
                int.TryParse(kvp[0], out userid);
                userUrl = kvp[1];
                watchList.Add(userid, userUrl);
            }

            MindYourManners(watchList);
        }

        /// <summary>
        /// A v1 approach for tracking StackOverflow reputation.
        /// A longer term approach would be to use the available API
        /// https://api.stackexchange.com/docs
        /// http://stackoverflow.com/questions/6620165/how-to-parse-json-in-c
        /// </summary>
        /// <param name="watched">A dictionary of the ids and the urls to watch</param>
        public static void MindYourManners(Dictionary<int, string> watched)
        {
            UrlRetriever ur = null;
            SlimResponse sr;
            StackExchangeUserParser p = null;
            DateTime markIt = DateTime.Now;

            foreach (KeyValuePair<int, string> kvp in watched)
            {
                ur = new UrlRetriever();
                sr = ur.ScrapePage(kvp.Value);
                p = new StackExchangeUserParser(sr.Content);
                p.SnapshotTS = markIt;
                p.EntityID = kvp.Key;
                p.Parse();
            }
        }
    }
}
